package com.webbei.HAO.intercept;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.webbei.HAO.action.Login;

public class InterceptLogin extends AbstractInterceptor {
	
	private HttpServletRequest request;
	private String str;

	public String intercept(ActionInvocation invocation) throws Exception {

		if (Login.class == invocation.getAction().getClass())
			return invocation.invoke();// If it is a loginaction, is not to
										// intercept

		request = (HttpServletRequest) invocation.getInvocationContext().get(
				ServletActionContext.HTTP_REQUEST);

		Map session = invocation.getInvocationContext().getSession();

		Cookie[] cooks = request.getCookies();
		System.out.println("cook.len:" + cooks + "   " + cooks.length);
		if (cooks != null) {
			System.out.println("you cook");
			for (Cookie cook : cooks) {
				if (cook.getName().equals("cookmail")) {
					str = "tiao";
					System.out.println("cookname:" + cook.getValue());
				}
				break;
			}
		} else if (session.get("cus") != null) {
			System.out.println("sesion is exist");
			return invocation.invoke();
		} else {
			System.out.println("liangze  doubucun zai");
			str = "notlogin";
		}

		if (Login.class == invocation.getAction().getClass())
			return invocation.invoke();

		return str;
	}

}
